<?php
class Vnews extends CI_Model {
	
	var $Title = "";
	var $Author = "";
	var $Detail = "";
	var $Type = "";
	var $ImgDir = "";
	var $NewsDate = '';

    function __construct()
    {
        parent::__construct();
    }
	    
    function get_last_n_entries($table, $type, $num=5)
    {
        $query = $this->db->order_by('NewsId', 'desc');
		$query = $query->get_where($table, array('Type' => $type), $num);
        return $query->result();
    }
	
	function get_entries($table, $limit, $offset, $type)
    {
        $query = $this->db->order_by('NewsId', 'desc');
		$query = $query->get_where($table, array('Type' => $type), $limit, $offset);
        return $query->result();
    }
	
	function get_entrie($table, $id)
    {
        $query = $this->db->where('NewsId', $id);
		$query = $query->get($table);
        return $query->result();
    }
	
	function get_total_num($table, $type)
	{
		$query = $this->db->where('Type', $type);
		return $this->db->count_all_results($table);
	}


    function insert_entry()
    {
        $this->Title = $this->input->post('title');
		$this->Author = $this->input->post('author');
		$this->Detail = $this->input->post('p-detail');
		$this->Type = $this->input->post('type');
		$this->ImgDir = $this->input->post('imgfile');
		$this->NewsDate = $this->input->post('date');
		$table = $this->input->post('table');
        return $this->db->insert($table, $this);
    }

	//function update_entry($table, $title, $date, $author, $id)
    function update_entry()
    {
		$this->Title = $this->input->post('title');
		$this->Author = $this->input->post('author');
		$this->Detail = $this->input->post('p-detail');
		$this->Type = $this->input->post('type');
		$this->ImgDir = $this->input->post('imgfile');
		$this->NewsDate = $this->input->post('date');
		$id = $this->input->post('id');
		$table = $this->input->post('table');
		//var_dump($this);
		//var_dump($id);
		//var_dump($table);
        $this->db->update($table, $this, array('NewsId' => $id));
    }
	
	function add_atts($id=-1)
    {
		if ($id < 0) {
			$id = $this->input->post('id');
		}
		$type = $this->input->post('type');
		
		if ($type == '中心动态' || $type == '学术专题' || $type == '公告通知' || $type == '精品课程')
			$news_type = 1;
		elseif ($type == '本科生教学' || $type == '研究生教学')
			$news_type = 2;
		elseif ($type == '硕士招生' || $type == '硕士生复试')
			$news_type = 3;
		
		$atts = $this->input->post('atts');
		
		foreach ($atts as $att) {
			$item = array('AppName' => $att, 'AppRealName' => $att, 'NewsID' => $id, 'NewsType' => $news_type);
			//var_dump($item);
			$this->db->insert('webpage_appendix', $item);
		}
    }
	
	function delete_atts()
    {		
		$atts = $this->input->post('delete_atts');
		
		foreach ($atts as $att) {
			$this->db->delete('webpage_appendix', array('AppId' => $att));
		}
    }
	
	function delete_entries()
    {		
		$entries = $this->input->post('delete');
		$table = $table = $this->input->post('table');
		
		$news_type = 0;
		if ($table == 'webpage_news')
			$news_type = 1;
		elseif ($table == 'webpage_graduate')
			$news_type = 2;
		elseif ($table == 'webpage_recruitnews')
			$news_type = 3;
		
		foreach ($entries as $entry) {
			$this->db->delete('webpage_appendix', array('NewsID' => $entry, 'NewsType'=> $news_type));
			$this->db->delete($table, array('NewsId' => $entry));
		}
    }
	
	function get_link($obj)
	{
		$ret = '<a href="' . site_url('/news/item');
		$ret .= '/' . $obj->NewsId;
		$ret .= '">' . $obj->Title;
		$ret .= '</a>';
		
		return $ret;
	}
	
	function get_all_atts($table, $id, $type)
	{
		$news_type = 0;
		if ($type == '中心动态' || $type == '学术专题' || $type == '公告通知' || $type == '精品课程')
			$news_type = 1;
		elseif ($type == '本科生教学' || $type == '研究生教学')
			$news_type = 2;
		elseif ($type == '硕士招生' || $type == '硕士生复试')
			$news_type = 3;
		
		$query = $this->db->get_where('webpage_appendix', array('NewsID' => $id, 'NewsType' => $news_type));
		
        return $query->result();
	}

}